#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>
#include "readwave.h"
#include "HarmonicFeat.h"

void main(int argc, char **argv)
{

#ifdef _DEBUG
	_CrtSetBreakAlloc(0);
#endif

	float	fInharm=0.f;
	float	fHarmSD=0.f;
	float	fOdd2Even=0.f;
	sTrist	Trist;
	
	char filename[256];
	strcpy(filename, argv[1]);
	
	int numSamples, nSmpRate;
	WavHead wavhead;
	short *WaveData = ReadWave(filename, &numSamples, &wavhead);
	nSmpRate = wavhead.SampleRate;
	
	SPECTRAL_HARMBASEINFO sp_baseinfo;	
	HarmonicFeat *Harmonic = new HarmonicFeat;
	Harmonic->Initialize(sp_baseinfo);
	
	int nFrameShift = sp_baseinfo.framePeriod*sp_baseinfo.smpRate/1.0e3 ;
	int nWinSize = sp_baseinfo.winSize*sp_baseinfo.smpRate/1.0e3;
	int numFrame = (numSamples-nWinSize)/nFrameShift+1;
	
	FILE *fp = fopen("Harm.txt", "w");
	int i;
	short *pCurWave=WaveData;
	for (i=0; i<numFrame; i++)
	{
		Harmonic->AddWavedata(pCurWave,nWinSize,nSmpRate);
		
		fInharm = Harmonic->Inharmonicity();
		fHarmSD = Harmonic->HarmSDeviation();
		fOdd2Even = Harmonic->Odd2Even();
		Trist = Harmonic->Tristimulus();
		

		
		fprintf(fp,"%.3f\t\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\n",fInharm, fHarmSD, fOdd2Even, Trist.T1, Trist.T2, Trist.T3);
		pCurWave += nFrameShift;
	}
	
	fclose(fp);

	delete []WaveData;
	delete Harmonic;

#ifdef _DEBUG
	_CrtDumpMemoryLeaks();
#endif
}